package com.dianrui.qiyouriding.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dianrui.qiyouriding.Constant;
import com.dianrui.qiyouriding.R;
import com.dianrui.qiyouriding.adapter.ChooseAddressAdapter;
import com.dianrui.qiyouriding.bean.AreaModel;
import com.dianrui.qiyouriding.dialog.WalletDialog;
import com.dianrui.qiyouriding.interfaces.OnAskDialogCallBack;
import com.dianrui.qiyouriding.okhttp.OkHttpRequest;
import com.dianrui.qiyouriding.util.StringUtils;
import com.dianrui.qiyouriding.util.ToastUtil;
import com.dianrui.qiyouriding.util.Url;
import com.google.gson.JsonObject;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 选择区域退押金
 */
public class ChooseAddressListActivity extends BaseActivity {
    private List<AreaModel> aeraModelList = new ArrayList<>();
    private ChooseAddressAdapter chooseAddressAdapter;


    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;
    @BindView(R.id.emptyView)
    LinearLayout emptyView;

    @Override
    public int getContentViewId() {
        return R.layout.address_list;
    }

    @Override
    public void initView() {
        customInit(true, R.color.main_color);
        title.setText(getString(R.string.despoit_money_title));
        LinearLayoutManager layoutManager = new LinearLayoutManager(ChooseAddressListActivity.this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(new DividerItemDecoration(ChooseAddressListActivity.this,LinearLayoutManager.VERTICAL));
        chooseAddressAdapter = new ChooseAddressAdapter(R.layout.choosearea_list_item, aeraModelList);
        recyclerView.setAdapter(chooseAddressAdapter);
        getAreaList();

        chooseAddressAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                final AreaModel areaModel = aeraModelList.get(position);
                switch (view.getId()){
                    case R.id.click_despoit_layout:
                        WalletDialog.createWalletDialog(ChooseAddressListActivity.this, getString(R.string.withdraws_txt), new OnAskDialogCallBack() {
                            @Override
                            public void onokey(Dialog dialog) {
                                dialog.dismiss();
                                Intent intent=new Intent(ChooseAddressListActivity.this, RefundDespotiSuccessActivity.class);
                                Bundle bundle=new Bundle();
                                bundle.putString("money", areaModel.price);
                                bundle.putString("settingid", areaModel.setting_id);
                                intent.putExtras(bundle);
                                startActivity(intent);
                                finish();
                            }

                            @Override
                            public void onCancel(Dialog dialog) {
                                dialog.dismiss();
                            }
                        });
                        break;
                    case R.id.withdraw_failed_btn:
                        Intent intent=new Intent(ChooseAddressListActivity.this, WithDrawFailedSubmitActivity.class);
                        startActivity(intent);
                        break;

                    default:
                        break;
                }
            }
        });
    }

    @Override
    public void initData() {}



    //获取押金的区域
    private void getAreaList() {
        JsonObject jsonObject = new JsonObject();
        if (!StringUtils.isEmpty(SPUtils.getInstance().getString("member_id"))) {
            jsonObject.addProperty("member_id", SPUtils.getInstance().getString("member_id"));
        }
        OkHttpRequest.getInstance().postRequests(Url.DEPOSITAREALIST, jsonObject.toString(),  new OkHttpRequest.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        final JSONObject jsonObject = new JSONObject(result);
                        if (Constant.DATASUCCESS.equals(jsonObject.optString("status"))) {
                            JSONArray jsonArray = jsonObject.optJSONArray("data");
                            if (jsonArray.length() > 0) {
                                for (int i = 0; i < jsonArray.length(); i++) {
                                    AreaModel areaModel = new AreaModel();
                                    areaModel.md_id = jsonArray.optJSONObject(i).optString("md_id");
                                    areaModel.setting_id = jsonArray.optJSONObject(i).optString("setting_id");
                                    areaModel.area_name = jsonArray.optJSONObject(i).optString("area_name");
                                    areaModel.price = jsonArray.optJSONObject(i).optString("deposit");
                                    areaModel.finance_id = jsonArray.optJSONObject(i).optInt("finance_id");
                                    aeraModelList.add(areaModel);
                                }
                                chooseAddressAdapter.setNewData(aeraModelList);
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    if (aeraModelList.size() <= 0) {
                        chooseAddressAdapter.getData().clear();
                        chooseAddressAdapter.setEmptyView(LayoutInflater.from(ChooseAddressListActivity.this).inflate(R.layout.empty_view,null));
                        chooseAddressAdapter.notifyDataSetChanged();
                    } else {
                        recyclerView.setVisibility(View.VISIBLE);
                    }
                }
            }

            @Override
            public void failedRequest(String result) {
                ToastUtil.showToast(getString(R.string.net_error));
            }
        });
    }


    @OnClick({R.id.back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
                default:
                    break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (aeraModelList != null) {
            aeraModelList.clear();
        }
    }
}
